package com.android.tea.shopper;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

import com.android.tea.R;
import com.android.tea.TeaEncyclopediaApp;
import com.android.tea.data.SettingLoader;

public class ShopperActivity extends TabActivity {
	protected static final String TAG = "FriendTabActivity";
	public TabHost tabHost;
	private String Recommend = "Recommend";
	private String Category = "Category";
	private String Options = "options";
	private String Nearby = "nearby";

	private TextView tab_allfriends;
	private TextView tab_contacts;
	private TextView tab_chart;
	private TextView tab_select;

	private TranslateAnimation tabBarAnimation;
	private int screenW;
	public static ShopperActivity instance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		TeaEncyclopediaApp.list.add(this);
		
		instance = this;
		setContentView(R.layout.shopper_tab_layout);
		initLayout();

		Intent intent = new Intent(this, GoodShopperActivity.class);
		intent.putExtra("mTitle", 0);
		intent.putExtra("mButton", 0);
		TabHost.TabSpec spec1 = tabHost.newTabSpec(Recommend)
				.setIndicator(Recommend).setContent(intent);
		tabHost.addTab(spec1);

		Intent contact = new Intent(this, ShopperClassifyActivity.class);
		TabHost.TabSpec spec2 = tabHost.newTabSpec(Category)
				.setIndicator(Category).setContent(contact);
		tabHost.addTab(spec2);

		TabHost.TabSpec spec3 = tabHost.newTabSpec(Options)
				.setIndicator(Options)
				.setContent(new Intent(this, ShopperOptionsActivity.class));
		tabHost.addTab(spec3);

		TabHost.TabSpec spec4 = tabHost.newTabSpec(Nearby).setIndicator(Nearby)
				.setContent(new Intent(this, ShopperNearbyActivity.class));
		tabHost.addTab(spec4);

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				onTabChange(tabId);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		int index = SettingLoader.getFriendTabIndex(this);
		String tabid = Recommend;
		if (index == 1) {
			tabid = Category;
		} else if (index == 2) {
			tabid = Options;
		} else if (index == 3) {
			tabid = Nearby;
		}
		onTabChange(tabid);
		tabHost.setCurrentTab(index);
	}

	private void initLayout() {
		tabHost = getTabHost();
		Options opts = new Options();
		opts.inJustDecodeBounds = true;
		// BitmapFactory.decodeResource(getResources(),
		// sR.drawable.header_friend_tab_bg, opts);
		int tabH = (int) (opts.outHeight
				* getResources().getDisplayMetrics().density / 2);
		tab_allfriends = (TextView) findViewById(R.id.tab_allfriends);
		tab_contacts = (TextView) findViewById(R.id.tab_contact);
		tab_chart = (TextView) findViewById(R.id.tab_chart);
		tab_select = (TextView) findViewById(R.id.tab_select);
		tab_allfriends.setHeight(tabH);
		tab_contacts.setHeight(tabH);
		tab_chart.setHeight(tabH);

		// tab_move_bar=(TextView) findViewById(R.id.tab_move_bar);
		// screenW=Utils.getScreenWidth(this);
		// tab_move_bar.setWidth(screenW/3);

		OnClickListener tabOnClicklistener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				int id = v.getId();
				switch (id) {
				case R.id.tab_allfriends:
					tabHost.setCurrentTabByTag(Recommend);
					break;
				case R.id.tab_contact:
					tabHost.setCurrentTabByTag(Category);
					break;
				case R.id.tab_chart:
					tabHost.setCurrentTabByTag(Options);
					break;
				case R.id.tab_select:
					tabHost.setCurrentTabByTag(Nearby);
					break;
				}
			}
		};
		tab_allfriends.setOnClickListener(tabOnClicklistener);
		tab_contacts.setOnClickListener(tabOnClicklistener);
		tab_chart.setOnClickListener(tabOnClicklistener);
		tab_select.setOnClickListener(tabOnClicklistener);

		// tabHost.setBackgroundResource(R.drawable.bitmap);
	}

	@Override
	public void onBackPressed() {
		// 退出程序
		Activity parent = this.getParent();
		if (parent != null) {
			parent.onBackPressed();
		} else {
			super.onBackPressed();
		}
	}

	int startX = 0;
	int endX = 0;

	public void onTabChange(String tabId) {
		int tabIndex = 0;
		tab_allfriends.setBackgroundColor(getResources().getColor(
				R.color.bg_green));
		tab_contacts.setBackgroundColor(getResources().getColor(
				R.color.bg_green));
		tab_chart.setBackgroundColor(getResources().getColor(R.color.bg_green));
		tab_select
				.setBackgroundColor(getResources().getColor(R.color.bg_green));

		tab_allfriends.setTextColor(getResources()
				.getColor(R.color.tab_default));
		tab_contacts.setTextColor(getResources().getColor(R.color.tab_default));
		tab_chart.setTextColor(getResources().getColor(R.color.tab_default));
		tab_select.setTextColor(getResources().getColor(R.color.tab_default));

		if (Recommend.equalsIgnoreCase(tabId)) {
			tabIndex = 0;
			tab_allfriends.setBackgroundResource(R.drawable.shop_item_bg);
			tab_allfriends.setTextColor(getResources().getColor(R.color.green));
		} else if (Category.equalsIgnoreCase(tabId)) {
			tabIndex = 1;
			tab_contacts.setBackgroundResource(R.drawable.shop_item_bg);
			tab_contacts.setTextColor(getResources().getColor(R.color.green));
		} else if (Options.equalsIgnoreCase(tabId)) {
			tabIndex = 2;
			tab_chart.setBackgroundResource(R.drawable.shop_item_bg);
			tab_chart.setTextColor(getResources().getColor(R.color.green));
		} else if (Nearby.equalsIgnoreCase(tabId)) {
			tabIndex = 3;
			tab_select.setBackgroundResource(R.drawable.shop_item_bg);
			tab_select.setTextColor(getResources().getColor(R.color.green));
		}
		// int width=tab_friends.getWidth();
		// if(width==0){
		// width=Utils.getScreenHeight(this)/3;
		// }
		// endX = width * (tabIndex);

		// tabBarAnimation = new TranslateAnimation(startX, endX, 0, 0);
		// tabBarAnimation.setDuration(200);
		// tabBarAnimation.setFillEnabled(true);
		// tabBarAnimation.setFillAfter(true);
		// tab_move_bar.startAnimation(tabBarAnimation);

		// startX=endX;

		SettingLoader.setFriendTabIndex(this, tabIndex);
	}

}